package com.cooper.scraper.show;

import java.util.Map.Entry;

import com.cooper.android.comm.LRULinkedHashMap.RemoveEldestEntryListener;
import com.cooper.android.comm.LRULinkedHashMap;
import com.cooper.android.comm.Resource;

import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.view.Display;

public abstract class FanartViewStrategy<T extends Resource>
		implements RemoveEldestEntryListener<String, Bitmap> {
	
	protected LRULinkedHashMap<String, Bitmap> cache = null;
	
	protected FanartImgStrategy fanartImgStrategy;
	

	public static final int WAIT_VIEW_TYPE = 0;
	public static final int FANART_VIEW_TYPE = 1;

	public abstract Bitmap getMediaFanart(T media, float fanartWidth,
			float fanartHeight);

	public void onReturnView(FanartViewHolder<T> holder, int type){	
	}

	public void onConverView(FanartViewHolder<T> holder){
	}

	public void init(FanartImgStrategy fanartImgStrategy, int showCount){
		this.fanartImgStrategy = fanartImgStrategy;
		if (cache != null) {
			cache.clear();
		}
		cache = new LRULinkedHashMap<String, Bitmap>(showCount * 3);
		cache.setRemoveEldestEntryListener(this);		
	}

	public void destroy(){
		reset();
	}

	public void reset(){
		if (cache != null)
			cache.clear();
	}

	public void configurationChanged(Configuration configuration,
			Display display, int showCount){
		reset();
		cache = new LRULinkedHashMap<String, Bitmap>(showCount * 5);
	}
	
	
	@Override
	public boolean onRemove(Entry<String, Bitmap> eldest) {
		Bitmap img = eldest.getValue();
		if (img != null && !img.isRecycled()) {
			img.recycle();
		}
		return true;
	}

}
